package view.panels 
{
	import comp.display.Label;
	import comp.display.TextArea;
	import comp.display.Window;
	import comp.event.CompEvent;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author Qi Donghui
	 */
	public class AddIdeaPanel extends Sprite 
	{
		private var _window:Window;
		
		private var _cont:Sprite;
		
		private var _tarea:TextArea;
		
		private var _tip:Label;
		
		private var _addPicBtn:SKaddPicBtn;
		private var _barBg:SKwindowBottomBarBg;
		private var _addIdeaBtn:SKaddIdeaConfirm;
		private var _cancelBtn:SKbtnCancel;
		private var _f:FileReference;
		private var _imgFilter:FileFilter;
		private var _picName:Label;
		private var _picPreviewBg:SKpicPriviewBg;
		private var _pic:DisplayObject;
		private var _picCont:Sprite;
		private var _picDeleteBtn:SKbtnDelete_s;
		
		public function AddIdeaPanel() 
		{
			
		}
		
		private function init():void
		{
			_window = new Window(this.stage, stage.stageWidth * 0.5 - 100, stage.stageHeight * 0.5 - 100, "添加创意：");
			_window.addEventListener(CompEvent.CLOSE, onWindowClose);
			_window.centerWindow();
			_window.setSize(405, 509 - 220);
			_window.centerWindow();
			
			_cont = new Sprite();
			var t1:Label = new Label(_cont, 30, 20, "创意描述：", new TextFormat("宋体", 12, 0x666666, true));
			_tarea = new TextArea(_cont, 30, 40, "");
			_tarea.setSize(343, 70);
			_addPicBtn = new SKaddPicBtn();
			_addPicBtn.addEventListener(MouseEvent.CLICK, onAddPicBtnClick);
			_addPicBtn.x = 30;
			_addPicBtn.y = _tarea.y + 85;
			_cont.addChild(_addPicBtn);
			_tip = new Label(_cont, 30, _addPicBtn.y + _addPicBtn.height + 15, "仅支持JPG、GIF、PNG图片文件，且文件小于5M", new TextFormat("宋体", 12, 0x797979));
			_barBg = new SKwindowBottomBarBg();
			_barBg.width = 405;		
			_cont.addChild(_barBg);
			_addIdeaBtn = new SKaddIdeaConfirm();
			_cancelBtn = new SKbtnCancel();
			_addIdeaBtn.x = 30;
			_cancelBtn.x = _addIdeaBtn.x + _addIdeaBtn.width + 15;		
			_cont.addChild(_addIdeaBtn);
			_cont.addChild(_cancelBtn);
			_window.source = _cont;
			resetWindow();
		}
		
		private function resetWindow():void
		{
			_tip.y =  _addPicBtn.y + _addPicBtn.height + 15;
			_barBg.y = 289 - _barBg.height - 34;
			_addIdeaBtn.y = _cancelBtn.y = _barBg.y + 12;
			
			if (_picName != null) 
			{
				if (_cont.contains(_picName)) {
					_cont.removeChild(_picName);
					_cont.removeChild(_picPreviewBg);
					_cont.removeChild(_picCont);
					_cont.removeChild(_picDeleteBtn);
				}
				if (_pic != null)
				{
					if (_picCont.contains(_pic)) _picCont.removeChild(_pic);
				}
			}
		}
		
		////////////////////////////////////////////////////////////
		//// 共有函数
		////////////////////////////////////////////////////////////	
		public function show():void
		{
			if (_window == null)
			{
				init();
			}else {
				stage.addChild(_window);
				_window.setSize(405, 509 - 220);
				_window.source = _cont;
				_window.draw();
				_window.centerWindow();
				
			}				
		}
		
		private function onWindowClose(e:CompEvent):void 
		{
			resetWindow();
		}		
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////			
		private function onAddPicBtnClick(e:MouseEvent):void 
		{
			if (_f == null)
			{
				_f = new FileReference();
				_f.addEventListener(Event.SELECT, onPicSelect);
				_f.addEventListener(Event.CANCEL, onPicCalcel);
				_imgFilter = new FileFilter("图片 (*.jpg, *.gif, *.jpeg, .*.png)", "*.jpg; *.gif; *.jpeg; *.png");
			}
			_f.browse([_imgFilter]);
		}		
		
		private function onPicCalcel(e:Event):void 
		{
			
		}
		
		private function onPicSelect(e:Event):void 
		{
			if (_picName == null)
			{
				_picName = new Label(null, 30, _addPicBtn.y + _addPicBtn.height + 10);
				_picPreviewBg = new SKpicPriviewBg();
				_picPreviewBg.x = 30;
				_picCont = new Sprite();
				_picDeleteBtn = new SKbtnDelete_s();
				_picDeleteBtn.x = _picPreviewBg.x + _picPreviewBg.width - _picDeleteBtn.width + 4;
				_picDeleteBtn.addEventListener(MouseEvent.CLICK, onPicDeleteBtnClick);
			}		
			while (_picCont.numChildren > 0) _picCont.removeChildAt(0);			
			_window.setSize(405, 509);
			_picName.text = _f.name;
			
			_picPreviewBg.y = _picName.y + 22;
			_picDeleteBtn.y = _picName.y;
			_cont.addChild(_picPreviewBg);
			_cont.addChild(_picName);
			_cont.addChild(_picDeleteBtn);
			_tip.y = _picName.y + 230;
			_barBg.y = 509 - _barBg.height - 34;
			_addIdeaBtn.y = _cancelBtn.y = _barBg.y + 12;
			
			_f.addEventListener(Event.COMPLETE, onLocalPicLoaded);
			_f.load();
		}
		
		private function onPicDeleteBtnClick(e:MouseEvent):void 
		{
			while (_picCont.numChildren > 0) _picCont.removeChildAt(0);		
			_window.setSize(405, 509 - 220);
			resetWindow();
		}
		
		private function onLocalPicLoaded(e:Event):void 
		{
			var ba:ByteArray = _f.data;
			var ld:Loader = new Loader();
			ld.contentLoaderInfo.addEventListener(Event.COMPLETE, onPicLoaded);
			ld.loadBytes(ba);
		}
		
		private function onPicLoaded(e:Event):void 
		{
			_pic = LoaderInfo(e.target).loader.content as DisplayObject;
			addChild(_pic);
			var sx:Number = (_picPreviewBg.width - 2) / _pic.width;
			var sy:Number = (_picPreviewBg.height - 2) / _pic.height;
			_pic.scaleX = _pic.scaleY = (sx > sy) ? sy : sx;
			_picCont.addChild(_pic);
			_picCont.x = _picPreviewBg.x + 1 + (_picPreviewBg.width - 2) * 0.5 - _picCont.width * 0.5;
			_picCont.y = _picPreviewBg.y + 1 + (_picPreviewBg.height - 2) * 0.5 - _picCont.height * 0.5;
			_cont.addChild(_picCont);
		}
		
	}

}